import { message } from "antd";
import apis from "./pushRulesApi";

export default {
  namespace: "pushRules",
  state: {
    list: [],
    totalCount: 0,
    totalPage: 0,
    showModal: false,
    filter: {
      ruleName: null,
      pageIndex: 1,
      pageSize: 10,
      ruleDesc: null,
      ruleType: null,
      expDate: null,
      status: null,
      order: null // "asc"升序  desc:降序
    }, // 搜索过滤器
    messageParameter: [],
    parameterTotalCount: 0,
    parameterTotalPage: 0,
    staticDatas: {}
  },
  effects: {
    // 推送规则查询
    *pushRuleConfQuery({ payload = {} }, { select, call, put }) {
      // 得到当前搜索过滤器的值
      const filter = yield select(state => state.pushRules.filter);
      // 当有payload参数不为空时，更新搜索过滤器的值
      const newFilter = { ...filter, ...payload };
      yield put({ type: "update/filter", payload: newFilter });
      // 用最新的搜索过滤器发送Http请求
      const { data, totalCount, totalPage } = yield call(
        apis.pushRuleConfQuery,
        newFilter
      );
      yield put({
        type: "save/data",
        payload: { data, totalCount, totalPage }
      });
    },
    // 推送规则新增
    *pushRuleConfAdd({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushRuleConfAdd, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushRuleConfQuery", payload: {} });
        message.success(errMsg);
        yield put({ type: "trigger/showModal", payload: false });
      }
    },
    // 推送规则删除
    *pushRuleConfDel({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushRuleConfDel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushRuleConfQuery", payload: {} });
        message.success(errMsg);
      }
    },
    // 推送规则修改
    *pushRuleConfMod({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushRuleConfMod, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushRuleConfQuery", payload: {} });
        message.success(errMsg);
        yield put({ type: "trigger/showModal", payload: false });
      }
    },
    // 推送规则状态修改
    *pushRuleStatusMod({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushRuleStatusMod, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushRuleConfQuery", payload: {} });
        message.success(errMsg);
      }
    },
    // 推送规则参数查询
    *queryMessageParameter({ payload = {} }, { call, put }) {
      const res = yield call(apis.queryMessageParameter, payload);
      const { data, parameterTotalCount, parameterTotalPage } = res;
      yield put({
        type: "save/messageParameter",
        payload: { data, parameterTotalCount, parameterTotalPage }
      });
    },
    // 获取静态数据
    *staticDatas(empty, { call, put }) {
      const { data: sceneList = [] } = yield call(apis.getSceneList);
      const { data: pushAppList = [] } = yield call(apis.getPushApp);
      yield put({
        type: "save/staticDatas",
        payload: { sceneList, pushAppList }
      });
    },
    *getTrigger({ payload }, { call, put }) {
      const { data: triggerList = [] } = yield call(apis.getTrigger, payload);
      yield put({
        type: "saveStaticData/triggerList",
        payload: {
          triggerList
        }
      });
    }
  },
  reducers: {
    "save/data": (state, { payload = {} }) => ({
      ...state,
      list: payload.data || [],
      totalCount: payload.totalCount || 0,
      totalPage: payload.totalPage || 0
    }),
    "update/filter": (state, { payload = {} }) => ({
      ...state,
      filter: payload
    }),
    "trigger/showModal": (state, { payload }) => ({
      ...state,
      showModal: payload
    }),
    "save/messageParameter": (state, { payload = {} }) => ({
      ...state,
      messageParameter: payload.data || [],
      parameterTotalCount: payload.totalCount || 0,
      parameterTotalPage: payload.totalPage || 0
    }),
    "save/staticDatas": (state, { payload = {} }) => ({
      ...state,
      staticDatas: { ...state.staticDatas, ...payload }
    }),
    "saveStaticData/triggerList": (state, { payload }) => ({
      ...state,
      staticDatas: { ...state.staticDatas, ...payload }
    })
  }
};
